package com.jamiexu.cn.chatwith.socket;

import com.jamiexu.cn.chatwith.Imp.IClientConnection;
import com.jamiexu.cn.chatwith.Imp.IConnectionListener;
import com.jamiexu.cn.chatwith.Imp.IOPProcessor;

import java.io.IOException;
import java.net.Socket;

public class ClientConnection implements IClientConnection, IOPProcessor {
    private Socket socket;
    private SocketProtocol socketProtocol;
    private String nickname;
    private IConnectionListener iConnectionListener;
    private boolean status = true;


    public ClientConnection(Socket socket, String nickname, IConnectionListener iConnectionListener) {
        this.socket = socket;
        this.nickname = nickname;
        this.iConnectionListener = iConnectionListener;
        this.socketProtocol = new SocketProtocol(this.socket);
        connect();
        new Thread(this::processOP).start();
        new Thread(this::processStatus).start();
    }

    @Override
    public void processOP() {
        while (status) {
            try {
                if (this.socket.isClosed()) {
                    status = false;
                    break;
                }
                Object[] objects = this.socketProtocol.getBlock();
//                System.out.println(Arrays.toString(objects));
                String op = objects[0] + "";
                if (op.equals(SocketProtocol.OP_CONNECTION_SERVER_CONNECT)) {
                    processConnect(this.nickname);
                } else if (op.equals(SocketProtocol.OP_CONNECTION_SERVER_DISCONNECT)) {
                    processDisconnect(this.nickname);
                } else if (op.equals(SocketProtocol.OP_MESSAGE_SERVER_SEND)) {
                    processMessage(objects[1] + "", objects[2] + "");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void processConnect(String nickname) {
//        this.iConnectionListener.connected(nickname);
    }

    @Override
    public void processDisconnect(String nickname) {
        disconnect();
//        this.iConnectionListener.disConnected(nickname);
    }

    @Override
    public void processMessage(String sender, String message) {
        if (message.equals("[系统消息]: " + sender + "进入聊天室")) {
            this.iConnectionListener.connected(sender);
            return;
        } else if (message.equals("[系统消息]: " + sender + "退出聊天室")) {
            this.iConnectionListener.disConnected(sender);
            return;
        }
        this.iConnectionListener.acceptMessage(sender, message);
    }

    @Override
    public void processStatus() {
        while (status) {
            try {
                this.socketProtocol.sendData(SocketProtocol.OP_CONNECTION_CLIENT_STATUS);
                Thread.sleep(5000);
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void connect() {
        try {
            this.socketProtocol.sendData(SocketProtocol.OP_CONNECTION_CLIENT_CONNECT, this.nickname);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void disconnect() {
        try {
            this.socketProtocol.sendData(SocketProtocol.OP_CONNECTION_CLIENT_DISCONNECT);
            this.socketProtocol = null;
            this.socket.close();
            System.gc();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sendMessage(String sender, String message) {
        try {
            this.socketProtocol.sendData(SocketProtocol.OP_MESSAGE_CLIENT_SEND, sender, message);
            System.out.println("Message sent");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Socket getSocket() {
        return socket;
    }
}
